﻿using System;
using Co9320.NamaAlert.Extension.Model;
using Co9320.NamaAlert.Extension.View;
using Co9320.NamaAlert.Extension.ViewModel;
using Co9320.NamaAlert.Model;

namespace Co9320.NamaAlert.Extension
{
    [Plugin("4e45bdce-527e-41d6-afc6-a80a4b909d78", HasAlertFunction = true, HasEditableOptions = true, RequireLiveProgramNotify = LiveProgramNotify.FavoriteOnly)]
    public class MailNotify : IPlugin
    {
        Setting setting;
        SendMail sendMail;

        public MailNotify()
        {
        }

        /// <summary>
        /// 初期化
        /// </summary>
        public void Initialize(IPluginSettings pluginSettings)
        {
            setting = new Setting(pluginSettings);
            sendMail = new SendMail(setting);
        }

        /// <summary>
        /// 設定ダイアログ
        /// </summary>
        /// <param name="dialogOwner"></param>
        public void EditOptions(System.Windows.Window dialogOwner)
        {
            OptionWindow window = new OptionWindow();
            OptionViewModel context = new OptionViewModel(setting);
            window.DataContext = context;

            if (window.ShowDialog() == true)
            {
                OnRequestSaveSettings(this, new SaveSettingsEventArgs(setting.PluginSettings));
            }
            return;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="program"></param>
        public void ExecuteAlert(LiveProgram liveProgram)
        {
            if(setting.ToMailAddress == "" && setting.FromMailAddress == "" && setting.SmtpServer != "")
            {
                try
                {
                    sendMail.Send(liveProgram.Title,
                        liveProgram.Uri.ToString() + "\n" +
                        liveProgram.Content + "\n" +
                        liveProgram
                        );
                }
                catch (Exception)
                {
                }
            }
            return;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="program"></param>
        public void ExecuteAction(LiveProgram liveProgram)
        {
            return;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="liveProgram"></param>
        public void ExecuteWhenLiveProgramStarted(LiveProgram liveProgram)
        {
            return;
        }

        #region イベント
        public event EventHandler<OpenUriEventArgs> RequestOpenUri;

        public event EventHandler<SaveSettingsEventArgs> RequestSaveSettings;

        public event EventHandler<ShowAlertWindowEventArgs> RequestShowAlertWindow;

        public event EventHandler<ShowBalloonTipEventArgs> RequestShowBalloonTip;

        public event EventHandler<SystemMessageEventArgs> SendSystemMessage;

        private void OnRequestSaveSettings(object sender, SaveSettingsEventArgs e)
        {
            if (RequestSaveSettings != null)
            {
                RequestSaveSettings(sender, e);
            }
        }
        #endregion
}
}
